package com.chenyun.system.controller;

import cn.hutool.core.date.DateUtil;
import com.chenyun.common.exceptions.BusinessException;
import com.chenyun.common.vo.AjaxResult;
import com.chenyun.store.aws.IOssClientService;
import com.chenyun.store.aws.OssFactory;
import com.chenyun.store.mapper.AttachmentMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;

/**
 * FileController：
 * Description:
 *
 * @author: Lonni
 * @date: 2022/7/28 0028 16:09
 */
@RestController
@RequestMapping("/file")
@Api(tags = "文件服务")
public class FileController {


    @Autowired
    AttachmentMapper attachmentMapper;




    @ApiOperation(value = "上传文件")
    @SneakyThrows
    @PostMapping("upload")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file) {
        IOssClientService ossClient = OssFactory.getOssClient();
        String upload = ossClient.upload(file.getBytes(), file.getOriginalFilename(), null);
        return AjaxResult.SUCCESS(upload);
    }


    @SneakyThrows
    @ApiOperation(value = "上传文件到本地")
    @PostMapping("uploadLocal")
    public AjaxResult uploadFileLocal(@RequestParam MultipartFile file){
        IOssClientService ossClient = OssFactory.getOssClientLocal();
        if (ossClient==null){
            throw new BusinessException("未设置本地存储配置信息");
        }
        String upload = ossClient.upload(file.getBytes(), file.getOriginalFilename(), null);
        return AjaxResult.SUCCESS(upload);

    }





    String getPath(String fileName) {
        Date date = new Date();
        String path = "common" + File.separator + DateUtil.year(date)
                + File.separator + DateUtil.month(date) + File.separator + DateUtil.dayOfMonth(date)
                + File.separator +fileName;

        return path;
    }

}
